<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>反馈描述项目</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="editForm" label-width="220px" ref="editForm">
          <el-form-item label="互动的管理成效自评：" prop="name">
            <el-input v-model="editForm.evaluation" auto-complete="off" placeholder="填写互动的管理成效自评(可不填)" type="textarea"
                      :autosize="{ minRows: 2, maxRows: 5 }" v-if="editStatus"></el-input>
            <span v-else>{{ editForm.evaluation || '--' }}</span>
          </el-form-item>
          <el-form-item label="未达成目标之反思及调整策略：" prop="name">
            <el-input v-model="editForm.strategy" auto-complete="off" placeholder="填写未达成目标之反思及调整策略(可不填)" type="textarea"
                      :autosize="{ minRows: 2, maxRows: 5 }" v-if="editStatus"></el-input>
            <span v-else>{{ editForm.strategy || '--' }}</span>
          </el-form-item>
          <el-form-item label="好的建议或感想、分享：" prop="name">
            <el-input v-model="editForm.share" auto-complete="off" placeholder="填写好的建议或感想、分享(可不填)" type="textarea"
                      :autosize="{ minRows: 2, maxRows: 5 }" v-if="editStatus"></el-input>
            <span v-else>{{ editForm.share || '--' }}</span>
          </el-form-item>
          <el-form-item label="回执文件：" prop="name" v-if="getList.length">
            <div v-for="item in getList">
              <btn_file :option="item"></btn_file>
            </div>
          </el-form-item>
          <el-form-item label="选择上传附件：" v-if="editStatus">
            <uploadFileModle number="20" @getFile="getFile" ref="uploadFileModle" :sycnStatus="true"
                             fileType="interaction_add" :fileListPush="getList"></uploadFileModle>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small">返回</el-button>
        <el-button size="small" type="primary" @click.native="editSubmit" v-if="Number(acticveWorke)!==3">
          {{ editStatus ? '确定并保存' : '编辑互动内容' }}
        </el-button>
        <el-button @click="editStatus=false" size="small" v-if="Number(acticveWorke)!==3 && commonOption.is_end=='完成' && editStatus">取消编辑
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {interactionPush} from "@/api/admin/interaction"

export default {
  props: ['visible', 'workId', 'commonOption', 'acticveWorke'],
  components: {},
  data() {
    return {
      dialogVisible: false,
      editStatus: true, // 是否编辑
      editForm: {
        evaluation: '',
        strategy: '',
        share: ''
      }, // 编辑界面数据
      fileList: [],
      getList: []
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        //acticveWorke 1 我的工作 2 我的发布 3 总数据预览
        // isPreview 只是预览
        console.log('acticveWorke:', this.acticveWorke)
        console.log('commonOption:', this.commonOption)
        if (this.commonOption.is_end === '完成') {
          this.editStatus = false
        } else {
          this.editStatus = true
        }
        this.InitFn()
      } else {
        this.editForm = {
          evaluation: '',
          strategy: '',
          share: ''
        }
        this.fileList = []
      }
    }
  },
  created() {
  },
  methods: {
    InitFn() {
      let data_ = this.getJson(this.commonOption)
      if (data_.evaluate) {
        this.editForm = JSON.parse(data_.evaluate)
      }
      if (data_.file) {
        this.fileList = JSON.parse(data_.file)
        this.getList = JSON.parse(data_.file)
      }
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    getFile(option) {
      console.log('收到附件数据：', option)
      this.fileList = this.getJson(option)
    },
    // 提交数据
    async editSubmit() {
      // work_id 	整型 	必填 	1 	- 	工作id
      // file 	整型 	必填 	1 	- 	文件地址
      // evaluate 	整型 	必填 	1 	- 	评价
      // 验证信息
      if (!this.editStatus) {
        this.editStatus = true
        return
      }
      let dataOption = this.getJson(this.editForm)
      console.log('dataOption:', this.fileList, dataOption)
      if (!this.fileList.length && !dataOption.evaluation && !dataOption.strategy && !dataOption.share) {
        this.$message.error('请填写至少一项内容')
        return
      }
      let file = []
      for (let item of this.fileList) {
        let a = {}
        a.name = item.name
        a.src = item.src
        file.push(a)
      }
      console.log('内容tijiao：', file)
      interactionPush({
        work_id: this.workId,
        file: JSON.stringify(file),
        evaluate: JSON.stringify(dataOption)
      }).then(res => {
        if (!res.code) {
          this.$message.success('编辑成功！')
          this.$emit('InitFn')
          this.closePOpup()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 50%;
  height: 550px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px 10px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.hearder-search {
  display: flex;
  align-items: center;
}
</style>


